﻿Imports System.Data.OleDb

Public Class EditPlayer
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim playerID As String = Request.Params("PlayerID")

        If Not IsPostBack Then
            If Not String.IsNullOrEmpty(playerID) Then
                Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
                oleDbConn.Open()

                Dim selectPlayerSQL As String = "SELECT * FROM Players WHERE ID=@f1"
                Dim selectPlayerCmd As OleDbCommand = New OleDbCommand(selectPlayerSQL, oleDbConn)
                selectPlayerCmd.CommandType = CommandType.Text
                selectPlayerCmd.Parameters.AddWithValue("@f1", Request.Params("PlayerID"))

                Dim selectPlayerDr As OleDbDataReader = selectPlayerCmd.ExecuteReader()

                If selectPlayerDr.HasRows Then
                    selectPlayerDr.Read()

                    If Not IsDBNull(selectPlayerDr("FullName")) Then
                        lblPlayerName.Text = selectPlayerDr("FullName")
                        txtFullName.Text = selectPlayerDr("FullName")
                    End If

                    If Not IsDBNull(selectPlayerDr("ClubID_FK")) Then ddlClubName.SelectedValue = selectPlayerDr("ClubID_FK")
                    If Not IsDBNull(selectPlayerDr("DateOfBirth")) Then txtDateOfBirth.Text = selectPlayerDr("DateOfBirth")
                    If Not IsDBNull(selectPlayerDr("NationalityID_FK")) Then ddlNationality.SelectedValue = selectPlayerDr("NationalityID_FK")
                    If Not IsDBNull(selectPlayerDr("Height")) Then txtHeight.Text = selectPlayerDr("Height")
                    If Not IsDBNull(selectPlayerDr("Weight")) Then txtWeight.Text = selectPlayerDr("Weight")
                    If Not IsDBNull(selectPlayerDr("PositionID_FK")) Then ddlPosition.SelectedValue = selectPlayerDr("PositionID_FK")
                    If Not IsDBNull(selectPlayerDr("PreferredFoot")) Then rblPreferredFoot.SelectedValue = selectPlayerDr("PreferredFoot")
                Else
                    Session("UserMessage_Error") = "The specified Player does not exist!"
                    Response.Redirect("~/ClubList.aspx")
                End If

                selectPlayerCmd.Dispose()
                oleDbConn.Close()
                oleDbConn.Dispose()
            Else
                Session("UserMessage_Error") = "A Player must be specified!"
                Response.Redirect("~/ClubList.aspx")
            End If
        End If

    End Sub

    Protected Sub btnBackPlayer_Click(sender As Object, e As EventArgs) Handles btnBackPlayer.Click
        Response.Redirect("~/PlayerDetail.aspx?PlayerID=" + Request.Params("PlayerID"))
    End Sub

    Protected Sub btnSaveChanges_Click(sender As Object, e As EventArgs) Handles btnSaveChanges.Click
        If Page.IsValid Then
            Dim myGUID As New System.Guid
            myGUID = Guid.NewGuid

            Dim newFileName = myGUID.ToString + ".jpg"

            If fuProfilePicture.HasFile Then
                Dim picLocationOnServerHardDisk = Request.MapPath("Players") & "/" & newFileName
                fuProfilePicture.SaveAs(picLocationOnServerHardDisk)
            End If

            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ClubsConnectionString").ConnectionString)
            Dim updatePlayerSQL As String

            If fuProfilePicture.HasFile Then
                updatePlayerSQL = "UPDATE Players SET FullName=@f1,ClubID_FK=@f2,DateOfBirth=@f3,NationalityID_FK=@f4,Height=@f5,Weight=@f6," +
                                          "PositionID_FK=@f7,PreferredFoot=@f8,ProfilePicture=@f9,Username=@f10,UpdatedAt=@f11 WHERE ID=@f12"
            Else
                updatePlayerSQL = "UPDATE Players SET FullName=@f1,ClubID_FK=@f2,DateOfBirth=@f3,NationalityID_FK=@f4,Height=@f5,Weight=@f6," +
                                          "PositionID_FK=@f7,PreferredFoot=@f8,Username=@f10,UpdatedAt=@f11 WHERE ID=@f12"
            End If

            Dim updatePlayerCmd As OleDbCommand = New OleDbCommand(updatePlayerSQL, oleDbConn)
            updatePlayerCmd.CommandType = CommandType.Text
            updatePlayerCmd.Parameters.AddWithValue("@f1", txtFullName.Text)
            updatePlayerCmd.Parameters.AddWithValue("@f2", ddlClubName.SelectedValue)
            updatePlayerCmd.Parameters.AddWithValue("@f3", txtDateOfBirth.Text)
            updatePlayerCmd.Parameters.AddWithValue("@f4", ddlNationality.SelectedValue)
            updatePlayerCmd.Parameters.AddWithValue("@f5", txtHeight.Text)
            updatePlayerCmd.Parameters.AddWithValue("@f6", txtWeight.Text)
            updatePlayerCmd.Parameters.AddWithValue("@f7", ddlPosition.SelectedValue)

            If rblPreferredFoot.SelectedValue = "Left" Then
                updatePlayerCmd.Parameters.AddWithValue("@f8", "Left")
            Else
                updatePlayerCmd.Parameters.AddWithValue("@f8", "Right")
            End If

            If fuProfilePicture.HasFile Then
                updatePlayerCmd.Parameters.AddWithValue("@f9", "Players/" & newFileName)
            End If

            updatePlayerCmd.Parameters.AddWithValue("@f10", User.Identity.Name)
            updatePlayerCmd.Parameters.AddWithValue("@f11", DateTime.Now.ToString("dd-MM-yyyy HH:mm:ss"))
            updatePlayerCmd.Parameters.AddWithValue("@f12", Request.Params("PlayerID"))

            oleDbConn.Open()
            updatePlayerCmd.ExecuteNonQuery()

            System.Threading.Thread.Sleep(500)
            Session("UserMessage") = "The player was successfully updated!"
            Response.Redirect("~/PlayerDetail.aspx?PlayerID=" + Request.Params("PlayerID"))

            updatePlayerCmd.Dispose()
            oleDbConn.Close()
            oleDbConn.Dispose()
        Else
            Session("UserMessage_Error") = "There was a problem with the data you submitted. Please try again."
            Response.Redirect(Request.RawUrl)
        End If
    End Sub

End Class